import { requestClient } from '#/api/request';
import type {
  CreateRoleDto,
  GetRolesInput,
  GetRolesOutput,
  GetPermissionsOutput,
  RoleDto,
  UpdatePermissionsDto,
  UpdateRoleDto,
} from './types';

const apiPrefix = '/api/identity/roles';

// 获取角色列表
export function getRoles(params?: GetRolesInput) {
  return requestClient.get<GetRolesOutput>(apiPrefix, {
    params,
  });
}

// 获取角色详情
export function getRole(id: string) {
  return requestClient.get<RoleDto>(`${apiPrefix}/${id}`);
}

// 创建角色
export function createRole(data: Partial<CreateRoleDto>) {
  return requestClient.post<RoleDto>(apiPrefix, data);
}

// 更新角色
export function updateRole(id: string, data: Partial<UpdateRoleDto>) {
  return requestClient.put<RoleDto>(`${apiPrefix}/${id}`, data);
}

// 删除角色
export function deleteRole(id: string) {
  return requestClient.delete(`${apiPrefix}/${id}`);
}

// 获取所有角色（不分页）
export function getAllRoles() {
  return requestClient.get<{ items: RoleDto[] }>(`${apiPrefix}/all`);
}

// 获取角色权限
export function getRolePermissions(providerName: string, providerKey: string) {
  return requestClient.get<GetPermissionsOutput>('/api/permission-management/permissions', {
    params: {
      providerName,
      providerKey,
    },
  });
}

// 更新角色权限
export function updateRolePermissions(
  providerName: string,
  providerKey: string,
  data: UpdatePermissionsDto,
) {
  return requestClient.put(
    '/api/permission-management/permissions',
    data,
    {
      params: {
        providerName,
        providerKey,
      },
    },
  );
}
